<?php


namespace app\middleware;
use think\facade\Db;

class ReturnData
{
    /**
     * @param $request
     * @param \Closure $next
     * @return mixed
     * https://blog.csdn.net/qq_19598963/article/details/111032496?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164949356016780271916873%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164949356016780271916873&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-4-111032496.142^v7^pc_search_result_control_group,157^v4^control&utm_term=thinkphp%E5%90%8E%E7%BD%AE%E4%B8%AD%E9%97%B4%E4%BB%B6%E8%BF%94%E5%9B%9E&spm=1018.2226.3001.4187
     * 处理返回数据格式中间件
     * https://blog.csdn.net/qq_35606400/article/details/116706926?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164949356016780271916873%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164949356016780271916873&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-116706926.142^v7^pc_search_result_control_group,157^v4^control&utm_term=thinkphp%E5%90%8E%E7%BD%AE%E4%B8%AD%E9%97%B4%E4%BB%B6%E8%BF%94%E5%9B%9E&spm=1018.2226.3001.4187
     */
//    public function handle($request, \Closure $next)
//    {
//
//        // 添加中间件执行代码
//        $response = $next($request);
//
//        //'application/json; charset=utf-8
//        //var_export($response);
//        if (method_exists($response, 'header') && in_array('text/html; charset=utf-8', $response->getHeader()) && !in_array($request->action(),['html','web_register','choose_system','notifyurl','withdraw','share_view','ajax'])) {
//            $data = $response->getData();
////            $data['debug'][] = ['data' => $request->data];
////            $data['debug'][] = ['time' => microtime(true) - $request->start_time];
////            $data['debug'][] = ['sql' => DB::getlastsql()];
//            $response = $response->data(abort(json($data)));
//        }
//        return $response;
//    }

    public function handle($request, \Closure $next)
    {

        // 添加中间件执行代码
        $response = $next($request);

        //'application/json; charset=utf-8
        //var_export($response);

            $data = $response->getData();

            if(empty($data)){
                return $response;
            }else{
                $response = $response->data(abort(json($data)));
            }
//            $data['debug'][] = ['data' => $request->data];
//            $data['debug'][] = ['time' => microtime(true) - $request->start_time];
//            $data['debug'][] = ['sql' => DB::getlastsql()];



    }
}